package com.example.zsfinacialmanage.view;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.example.zsfinacialmanage.db.MyDBHelper;
import com.example.zsfinacialmanage.R;
public class LoginActivity extends AppCompatActivity {
    private EditText et_name,et_pwd;
    private Button btn_login,btn_newregister;
    private MyDBHelper mhelper;
    private SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        btnnewRegister();
        btnLogin();
    }
    public void initView(){
        // 在初始化部分修改获取EditText的id，根据布局文件中的id进行调整
        et_name = findViewById(R.id.et_name_lg);
        et_pwd = findViewById(R.id.et_pwd_lg);
        btn_login = findViewById(R.id.bt_login_lg);
        btn_newregister = findViewById(R.id.bt_newregister_lg);
        mhelper = new MyDBHelper(LoginActivity.this);
        db = mhelper.getWritableDatabase();
    }
    private void btnLogin() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String inputname = et_name.getText().toString();
                String inputpwd = et_pwd.getText().toString();
                if (inputname.equals("") || inputpwd.equals("")) {
                    Toast.makeText(LoginActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    Cursor cursor = db.rawQuery("select * from tb_userinfo where name=? and pwd=?", new String[]{inputname, inputpwd});
                    if (cursor.moveToNext()) {
                        // 登录成功逻辑
                        SharedPreferences.Editor editor = getSharedPreferences("userinfo", 0).edit();
                        editor.putString("username", inputname);
                        editor.putString("userpwd", inputpwd);
                        editor.apply();
                        Toast.makeText(LoginActivity.this, "登陆成功！", Toast.LENGTH_SHORT).show();

                        // 启动MainActivity
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(LoginActivity.this, "用户名或密码错误,请重新输入", Toast.LENGTH_SHORT).show();
                        et_name.setText("");
                        et_pwd.setText("");
                    }
                }
            }
        });
    }
        private void btnnewRegister() {
            btn_newregister.setOnClickListener(new View.OnClickListener(){
                @Override
            public void onClick(View v) {
                    Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                    startActivity(intent);
                    finish();
                }
            });
        }
}